package awd.bj.jls.webs.service;


import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import awd.bj.base.model.Variables;
import awd.bj.jls.webs.api.JlsServersApi;
import awd.bj.jls.webs.api.WorkFlowApi;
import awd.bj.jls.webs.utils.CacheUtils;
import awd.bj.jls.webs.utils.Result;
import awd.bj.jls.webs.utils.ResultUtils;
import awd.framework.common.utils.StringUtils;


/**
 * @program: awd.bj.kss
 * @description:
 * @author: WS
 * @create: 2019-01-21
 **/
@Component
public class ProcessService {
    @Autowired
    private WorkFlowApi workFlowApi;
    @Autowired
    private JlsServersApi jlsServersApi;

    /**
     * 流程互斥判断||时隔限制||每月办理次数限制
     *
     * @param jsbh
     * @param rybh
     * @param key       (key=user.jsbh+....)
     * @param tableName 数据库表名
     * @return
     */
    public Result<?> FlowMutex(String jsbh, String rybh, String key, String tableName) {
        String mapmutex = null;
        String monthlylimit = null;
        String timelimit = null;
        try {
            JSONObject jsonObject = CacheUtils.get().getValueByKey(key);
            System.err.println("jsonObject=="+jsonObject);
            if (!StringUtils.isNullOrEmpty(jsonObject)) {
            	if(jsonObject.get("mapmutex")!=null) {
            		mapmutex = jsonObject.get("mapmutex").toString();
            	}
            	if(jsonObject.get("monthlylimit")!=null) {
            		monthlylimit = jsonObject.get("monthlylimit").toString();
            	}
            	if(jsonObject.get("timelimit")!=null) {
            		timelimit = jsonObject.get("timelimit").toString();
            	}
            }
            Variables variables = new Variables();
            variables.setRybh(rybh);
            variables.setJsbh(jsbh);
            if (!StringUtils.isNullOrEmpty(mapmutex)) {
                String[] str = mapmutex.split(",");
                for (int i = 0; i < str.length; i++) {
                    variables.setProcessDefinitionKey(str[i]);
                    List<Map<String, Object>> obj = (List<Map<String, Object>>) workFlowApi.findPersonalTaskList("admin", "1", variables).getResult();
                    if (obj != null && obj.size() != 0) {                   	
                        JSONObject json = CacheUtils.get().getValueByKey(obj.get(0).get("processDefinitionKey").toString());
                        return ResultUtils.error("错误！当前人员正在办理<b>" + json.get("memo").toString() + "</b></br>中的<b>" + obj.get(0).get("name") + "</b>");
                    }
                }            }
            // TODO 流程办理数量验证
            /*int num = kssBaseApis.VerificationHour(new HashMap<String, Object>() {{
                put("tableName", tableName);
                put("jsbh", jsbh);
                put("rybh", rybh);
            }}).getResult();
            int count = kssBaseApis.VerificationMonth(new HashMap<String, Object>() {{
                put("tableName", tableName);
                put("jsbh", jsbh);
                put("rybh", rybh);
            }}).getResult();
            if (num > 1) {
                return ResultUtils.error("当前人员办理频率过高，请稍后再试！");
            }
            if(monthlylimit!=null) {
            	if (count > Integer.parseInt(monthlylimit)) {
            		return ResultUtils.error("当前人员当月办理次数超出上限,请下月再试！");
            	}
            }*/
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.error("流程判断失败，请联系管理员！");
        }
        return ResultUtils.success();
        }
    
    /**
     * 判断人员是否已经在流程中
     * @param flowkey
     * @param rybh
     * @param jsbh
     * @return
     */
    public Result<?> FlowSingle(String flowkey, String rybh, String jsbh){
    	String processDefinitionKey = "";
    	if(!StringUtils.isNullOrEmpty(flowkey)) {
    		processDefinitionKey = flowkey.split(":")[0];
    	}else {
    		return ResultUtils.error("未传递必要参数");
    	}
    	Variables variables = new Variables();
        variables.setRybh(rybh);
        variables.setJsbh(jsbh);
        variables.setProcessDefinitionKey(processDefinitionKey);
        System.err.println(processDefinitionKey+"-----------------");
		List<Map<String, Object>> obj = (List<Map<String, Object>>) workFlowApi.findPersonalTaskList("admin", "1", variables).getResult();
		if (obj.size()>0) {
			return ResultUtils.error("已存在于该流程中，无法重新办理！！");
		}
        return ResultUtils.success();
    }
}
